﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExamText
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DisPlay();
            }
        }

        private void DisPlay()
        {
            string sql = "select * from Articles";
            GridView1.DataSource = DbHelper.GetDataTable(sql);
            GridView1.DataBind();
        }

        private string GetValue(int rowIndex,int colnum)
        {
            var value = ((TextBox)GridView1.Rows[rowIndex].Cells[colnum].Controls[0]).Text.Trim();
            return value;
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            DisPlay();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var isOk = int.TryParse(GridView1.DataKeys[e.RowIndex].Value.ToString(), out int id);
            var title = GetValue(e.RowIndex, 1);
            var content = GetValue(e.RowIndex, 2);
            var author = GetValue(e.RowIndex, 3);
            var remarks = GetValue(e.RowIndex, 4);

            string sql;

            if (isOk)
            {
                sql = string.Format("update Articles set Title='{0}',Content='{1}',Author='{2}',Remarks='{3}' where Id={4}", title, content, author, remarks, id);
            }
            else
            {
                sql = string.Format("insert into Articles(Title,Content,Author,Remarks) values('{0}','{1}','{2}','{3}')",title,content,author,remarks);
            }

            var res = DbHelper.UpdateData(sql);

            if (res == 1)
            {
                Response.Write("<script>alert('更新数据成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('更新数据失败')</script>");
            }


            GridView1.EditIndex = -1;
            DisPlay();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            DisPlay();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GridView1.DataKeys[e.RowIndex].Value.ToString();

            string sql = "delete Articles where Id=" + id + "";

            var res = DbHelper.UpdateData(sql);

            if (res == 1)
            {
                Response.Write("<script>alert('删除成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }

            DisPlay();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string sql = "select * from Articles";
            DataTable table = DbHelper.GetDataTable(sql);
            table.Rows.Add(table.NewRow());

            GridView1.EditIndex = GridView1.Rows.Count;
            GridView1.DataSource = table;
            GridView1.DataBind();


        }
    }
}